package com.murphy.algorithm.likou;

import com.alibaba.fastjson.JSON;

import java.util.ArrayList;
import java.util.List;

/**
 * 归并排序
 * @author dongsufeng
 * @version 4.0
 * @date 2023/3/1 16:36
 */
public class MergeSort {

    public List<Integer> mergeSort1(int[] n1,int[] n2){
        List<Integer> result = new ArrayList<>();
        int i = 0;
        int j = 0;
        while (i < n1.length && j < n2.length){
            if (n1[i] > n2[j]){
                result.add(n2[j]);
                ++ j;
            }else {
                result.add(n1[i]);
                ++ i;
            }
        }
        while (i < n1.length){
            result.add(n1[i]);
            ++ i;
        }
        while (j < n2.length){
            result.add(n2[j]);
            ++ j;
        }
        return result;
    }


    public static void main(String[] args) {
        int[] n1 = {1,3,5,7,9,14,24,56};
        int[] n2 = {2,5,8,23,67,123};
        MergeSort mergeSort = new MergeSort();
        System.out.println(JSON.toJSON(mergeSort.mergeSort1(n1,n2)));
    }
}
